package com.chexiehui.jc.widget;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aries.ui.view.radius.RadiusTextView;
import com.chexiehui.jc.R;
import com.chexiehui.jc.entity.SpecialCarInfo;
import com.chexiehui.jc.utils.GlideUtil;
import com.lxj.xpopup.core.BottomPopupView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class CustomSpecialCarInquiryPopup extends BottomPopupView {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_old_price)
    TextView tvOldPrice;
    @BindView(R.id.iv_popup_close)
    ImageView ivPopupClose;
    @BindView(R.id.iv_image)
    ImageView ivImage;
    @BindView(R.id.tv_merchant_pre)
    TextView tvMerchantPre;
    @BindView(R.id.tv_merchant_name)
    TextView tvMerchantName;
    @BindView(R.id.tv_merchant_address)
    TextView tvMerchantAddress;
    @BindView(R.id.tv_time_pre)
    RadiusTextView tvTimePre;
    @BindView(R.id.tv_merchant_time)
    TextView tvMerchantTime;
    @BindView(R.id.rtv_apply)
    RadiusTextView rtvApply;
    private Unbinder mUnBinder;
    private ICustomSpecialCarInquiryPopupListener listener;
    private SpecialCarInfo specialCarInfo;

    public CustomSpecialCarInquiryPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layput_special_inquiry_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mUnBinder = ButterKnife.bind(this);
    }

    public void initModel(SpecialCarInfo specialCarInfo) {
        this.specialCarInfo = specialCarInfo;
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
        setModelInfo();
    }

    public void setListener(ICustomSpecialCarInquiryPopupListener listener) {
        this.listener = listener;
    }

    private void setModelInfo() {
        if(specialCarInfo != null){
            GlideUtil.loadImageViewCrop(getContext(), specialCarInfo.getImg(), ivImage,
                    R.drawable.shape_square, R.drawable.shape_square);
            tvName.setText(specialCarInfo.getName());
            tvPrice.setText(specialCarInfo.getPriceStr());
            tvOldPrice.setText(specialCarInfo.getOldPriceStr());
            tvOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
            tvMerchantName.setText(specialCarInfo.getShopName());
            tvMerchantAddress.setText(specialCarInfo.getAdress());
            tvMerchantTime.setText(specialCarInfo.getStartEndTimeStr());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
    }

    @OnClick({R.id.iv_popup_close,R.id.rtv_apply})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_popup_close:
                dismiss();
                break;
            case R.id.rtv_apply:
                dismiss();
                if(listener != null){
                    listener.onSureInquiry(specialCarInfo);
                }
                break;
        }
    }

    public interface ICustomSpecialCarInquiryPopupListener {
        void onSureInquiry(SpecialCarInfo carInfo);
    }

    public void releaseObj() {
        listener = null;
    }
}
